package com.itheima.chapter03.controller;

import com.itheima.chapter03.domain.AjaxResult;
import com.itheima.chapter03.domain.Comment;
import com.itheima.chapter03.service.CommentService;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    //    @PostMapping("/comment")
    @PostMapping
    public AjaxResult insert(@Validated @RequestBody Comment comment) {
//        if (comment.getAId() == null || comment.getAId() <= 0) {
//            return AjaxResult.error("归属文章不存在");
//        }
//        if (StringUtils.isBlank(comment.getAuthor())) {
//            return AjaxResult.error("作者不能为空");
//        }
//        if (StringUtils.isBlank(comment.getContent())) {
//            return AjaxResult.error("评论内容不能为空");
//        }
        int ret = commentService.insert(comment);
        if (ret > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }
}
